package com.example.service.impl;

import com.example.entity.Count;
import com.example.entity.Floor;
import com.example.entity.Page.MyPageHelper;
import com.example.entity.Page.PageRequest;
import com.example.entity.Page.PageResult;
import com.example.mapper.FloorMapper;
import com.example.result.CommonResult;
import com.example.service.FloorService;
import com.example.utils.redis.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @project: smart-property
 * @description: 房屋管理 service 实现层
 * @author: cjq
 * @create: 2022-11-20 19:36
 **/
@Service
public class FloorServiceImpl implements FloorService {
    @Autowired
    FloorMapper floorMapper;

    @Autowired
    RedisUtil redisUtil;

    Integer cid;

    /**
     * 储物室查重
     * @param floor
     * @param uid
     * @return
     */
    @Override
    public CommonResult listLocker(String floor, Integer uid) {
        Floor floor1 = floorMapper.listLocker(floor, uid);
        if (!StringUtils.isEmpty(floor1)){
            return CommonResult.ok();
        }
        return CommonResult.fail(floor1);
    }

    /**
     * 添加/修改房屋信息
     * @param floor
     * @return
     */
    @Override
    public int saveFloorInfo(Floor floor) {
        int i = 0;
        String lockerInfo = "lockerInfo"+"_"+this.cid;
        String floorInfo = "floorInfo"+"_"+this.cid;
        if (floor.getId() == null){
            floor.setFloorNumber(floor.getFloorNumber()+"_"+floor.getBid()+"_"+floor.getUid()+"_"+floor.getFloor());
            floor.setDelFlag(0);
            if (StringUtils.isEmpty(floor.getStorageRoom())){
                floor.setStorageRoom("-1");
            }
            i = floorMapper.addFloorInfo(floor);
            redisUtil.del(lockerInfo);
            redisUtil.del(floorInfo);
        }else {
            floor.setFloorNumber(floor.getFloorNumber()+"_"+floor.getBid()+"_"+floor.getUid()+"_"+floor.getFloor());
            floor.setDelFlag(0);
            if (StringUtils.isEmpty(floor.getStorageRoom())){
                floor.setStorageRoom("-1");
            }
            i = floorMapper.updateFloorInfo(floor);
            redisUtil.del(lockerInfo);
            redisUtil.del(floorInfo);
        }
        return i;
    }

    /**
     * 储物室列表展示
     * @param floor
     * @param cid
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult queryLockerInfo(String floor, Integer uid,Integer cid, PageRequest pageRequest) {
        this.cid = cid;
        String lockerInfo = "lockerInfo"+"_"+this.cid;
        List<Floor> list = (List<Floor>) redisUtil.get(lockerInfo);
        if (floor == ""){
            if (list == null){
                list = floorMapper.queryLockerInfo(floor,uid,cid);
                redisUtil.set(lockerInfo,list);
            }
        }else {
            return MyPageHelper.getPageResult(floorMapper.queryLockerInfo(floor,uid,cid));
        }
        return MyPageHelper.getPageResult(list);
    }

    /**
     * 删除房屋信息
     * @param id
     * @return
     */
    @Override
    public int delFloorInfo(Integer id) {
        String lockerInfo = "lockerInfo"+"_"+this.cid;
        redisUtil.del(lockerInfo);
        String floorInfo = "floorInfo"+"_"+this.cid;
        redisUtil.del(floorInfo);
        return floorMapper.delFloorInfo(id);
    }

    /**
     * 批量删除房屋信息
     * @param ids
     * @return
     */
    @Override
    public int batchDel(Integer[] ids) {
        String lockerInfo = "lockerInfo"+"_"+this.cid;
        String floorInfo = "floorInfo"+"_"+this.cid;
        redisUtil.del(floorInfo);
        redisUtil.del(lockerInfo);
        return floorMapper.batchDel(ids);
    }

    /**
     * 房屋列表展示
     * @param purpose
     * @param building
     * @param pageRequest
     * @return
     */
    @Override
    public PageResult queryFloorInfo(String purpose, String building, Integer cid,PageRequest pageRequest) {
        this.cid = cid;
        String floorInfo = "floorInfo"+"_"+this.cid;
        List<Floor> list = (List<Floor>) redisUtil.get(floorInfo);
        if (purpose == "" && building == ""){
            if (list == null){
                list = floorMapper.queryFloorInfo(purpose,building,cid);
                redisUtil.set(floorInfo,list);
            }
        }else {
            return MyPageHelper.getPageResult(floorMapper.queryFloorInfo(purpose,building,cid));
        }
        return MyPageHelper.getPageResult(list);
    }

    /**
     * 统计房屋
     * @param cid
     * @return
     */
    @Override
    public Count countFloor(Integer cid) {
        return floorMapper.countFloor(cid);
    }
}
